<?php


namespace app\api\controller;

use app\model\member\MemberOpenAccount as MemberOpenAccountModel;

class Memberopenaccount extends BaseApi
{


    public function add()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $account_type = isset($this->params['account_type']) ? $this->params['account_type'] : 0;//开户类型,1企业0个人
        $enterprise_name = isset($this->params['enterprise_name']) ? $this->params['enterprise_name'] : '';//企业名称
        $province_id = isset($this->params['province_id']) ? $this->params['province_id'] : '';//
        $city_id = isset($this->params['city_id']) ? $this->params['city_id'] : '';//
        $district_id = isset($this->params['district_id']) ? $this->params['district_id'] : '';//
        $full_address = isset($this->params['full_address']) ? $this->params['full_address'] : '';//企业所在地
        $address = isset($this->params['address']) ? $this->params['address'] : '';//企业详细地址
        $tyshxydm = isset($this->params['tyshxydm']) ? $this->params['tyshxydm'] : '';//统一社会信用代码
        $shxyzyxq_time_type = isset($this->params['shxyzyxq_time_type']) ? $this->params['shxyzyxq_time_type'] : 0;//社会信用证有效期类型,1是永久
        $shxyzyxq_start_time = isset($this->params['shxyzyxq_start_time']) ? $this->params['shxyzyxq_start_time'] : '';//社会信用证开始日期
        $shxyzyxq_end_time = isset($this->params['shxyzyxq_end_time']) ? $this->params['shxyzyxq_end_time'] : '';//社会信用证结束日期
        $business_boundaries = isset($this->params['business_boundaries']) ? $this->params['business_boundaries'] : '';//经营范围
        $realname = isset($this->params['realname']) ? $this->params['realname'] : '';//姓名
        $mobile = isset($this->params['mobile']) ? $this->params['mobile'] : '';//手机号
        $id_card_num = isset($this->params['id_card_num']) ? $this->params['id_card_num'] : '';//身份证号
        $id_card_time_type = isset($this->params['id_card_time_type']) ? $this->params['id_card_time_type'] : 0;//身份证有效期类型,1是永久
        $id_card_start_time = isset($this->params['id_card_start_time']) ? $this->params['id_card_start_time'] : '';//身份证有效期的开始日期
        $id_card_end_time = isset($this->params['id_card_end_time']) ? $this->params['id_card_end_time'] : '';//身份证有效期的结束日期
        $qualification_img = isset($this->params['qualification_img']) ? $this->params['qualification_img'] : '';//资质图片
        $id_card_front_img = isset($this->params['id_card_front_img']) ? $this->params['id_card_front_img'] : '';//身份证正面图片
        $id_card_reverse_img = isset($this->params['id_card_reverse_img']) ? $this->params['id_card_reverse_img'] : '';//身份证反面图片
        $cardholder_id_card_num = isset($this->params['cardholder_id_card_num']) ? $this->params['cardholder_id_card_num'] : '';//持卡人证件号码
        $cardholder_mobile = isset($this->params['cardholder_mobile']) ? $this->params['cardholder_mobile'] : '';//持卡人手机号
        $cardholder_realname = isset($this->params['cardholder_realname']) ? $this->params['cardholder_realname'] : '';//持卡人姓名
        $bank_card_number = isset($this->params['bank_card_number']) ? $this->params['bank_card_number'] : '';//银行卡号
        $bank_type = isset($this->params['bank_type']) ? $this->params['bank_type'] : '';//银行卡类型,1对公,0对私
        $bank_card_name = isset($this->params['bank_card_name']) ? $this->params['bank_card_name'] : '';

        $member_open_account_model = new MemberOpenAccountModel();

        $info = $member_open_account_model->getMemberOpenAccountInfo([['member_id','=',$this->member_id]],'account_id,status');
        if (!empty($info['data'])){
            if ($info['data']['status']==1){
                return $this->response($this->error('', '已通过请勿重复提交'));
            }
        }
        if ($account_type==1){
            if (empty($enterprise_name)) {
                return $this->response($this->error('', '请输入企业名称'));
            }
            if (empty($address)) {
                return $this->response($this->error('', '请输入企业详细地址'));
            }
            if (empty($tyshxydm)) {
                return $this->response($this->error('', '请输入统一社会信用代码'));
            }
            if ($shxyzyxq_time_type==0) {
                if (empty($shxyzyxq_start_time)) {
                    return $this->response($this->error('', '请选择社会信用证开始日期'));
                }
                if (empty($shxyzyxq_end_time)) {
                    return $this->response($this->error('', '请选择社会信用证到期日期'));
                }
            }
            if (empty($business_boundaries)) {
                return $this->response($this->error('', '请输入经营范围'));
            }

            if (empty($realname)) {
                return $this->response($this->error('', '请输入法人姓名'));
            }
            if (empty($mobile)) {
                return $this->response($this->error('', '请输入法人手机号'));
            }
            if (!validatePhoneNumber($mobile)) {
                return $this->response($this->error('', '手机号有误'));
            }
            if (empty($id_card_num)) {
                return $this->response($this->error('', '请输入法人身份证号'));
            }


            if (!validateIDCard($id_card_num)) {
                return $this->response($this->error('', '法人身份证号无效'));
            }

            if (empty($qualification_img)) {
                return $this->response($this->error('', '请上传资质图片'));
            }
            if (empty($id_card_front_img)) {
                return $this->response($this->error('', '请上传身份证正面图片'));
            }
            if (empty($id_card_reverse_img)) {
                return $this->response($this->error('', '请上传身份证反面图片'));
            }
        } else {
            if (empty($realname)) {
                return $this->response($this->error('', '请输入姓名'));
            }
            if (empty($mobile)) {
                return $this->response($this->error('', '请输入手机号'));
            }
            if (!validatePhoneNumber($mobile)) {
                return $this->response($this->error('', '手机号有误'));
            }
            if (empty($id_card_num)) {
                return $this->response($this->error('', '请输入身份证号'));
            }
            if (!validateIDCard($id_card_num)) {
                return $this->response($this->error('', '身份证号无效'));
            }
        }

        if ($id_card_time_type==0) {
            if (empty($id_card_start_time)) {
                return $this->response($this->error('', '请选择身份证有效期的开始日期'));
            }
            if (empty($id_card_end_time)) {
                return $this->response($this->error('', '请选择身份证有效期的到期日期'));
            }
        }

        if (empty($cardholder_id_card_num)) {
            return $this->response($this->error('', '请输入持卡人证件号码'));
        }
        if (!validateIDCard($cardholder_id_card_num)) {
            return $this->response($this->error('', '持卡人证件号码无效'));
        }
        if (empty($cardholder_mobile)) {
            return $this->response($this->error('', '请输入持卡人手机号'));
        }
        if (!validatePhoneNumber($cardholder_mobile)) {
            return $this->response($this->error('', '持卡人手机号有误'));
        }
        if (empty($cardholder_realname)) {
            return $this->response($this->error('', '请输入持卡人姓名'));
        }
        if (empty($bank_card_name)) {
            return $this->response($this->error('', '请输入银行名称'));
        }
        if (empty($bank_card_number)) {
            return $this->response($this->error('', '请输入银行卡号'));
        }
        if (!validateBankCard($bank_card_number)) {
            return $this->response($this->error('', '银行卡号有误'));
        }

        $data = [
            'member_id' => $this->member_id,
            'account_type' => $account_type,
            'enterprise_name' => $enterprise_name,
            'province_id' => $province_id,
            'city_id' => $city_id,
            'district_id' => $district_id,
            'full_address' => $full_address,
            'address' => $address,
            'tyshxydm' => $tyshxydm,
            'shxyzyxq_time_type' => $shxyzyxq_time_type,
            'shxyzyxq_start_time' => $shxyzyxq_start_time,
            'shxyzyxq_end_time' => $shxyzyxq_end_time,
            'business_boundaries' => $business_boundaries,
            'realname' => $realname,
            'mobile' => $mobile,
            'id_card_num' => $id_card_num,
            'id_card_time_type' => $id_card_time_type,
            'id_card_start_time' => $id_card_start_time,
            'id_card_end_time' => $id_card_end_time,
            'qualification_img' => $qualification_img,
            'id_card_front_img' => $id_card_front_img,
            'id_card_reverse_img' => $id_card_reverse_img,
            'cardholder_id_card_num' => $cardholder_id_card_num,
            'cardholder_mobile' => $cardholder_mobile,
            'cardholder_realname' => $cardholder_realname,
            'bank_card_number' => $bank_card_number,
            'bank_card_name'=>$bank_card_name,
            'bank_type' => $bank_type,
            'create_time' => time()
        ];
        if (!empty($info['data'])){
            $res = $member_open_account_model->editMemberOpenAccount($data,[['account_id','=',$info['data']['account_id']]]);
        } else {
            $res = $member_open_account_model->addMemberOpenAccount($data);
        }

        return $this->response($res);
    }

    //获取开户信息
    public function info()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $member_open_account_model = new MemberOpenAccountModel();

        $info = $member_open_account_model->getMemberOpenAccountInfo([['member_id','=',$this->member_id]]);

        return $this->response($info);
    }
}